// pages/mine/mine.js
const app = getApp()
var base_url = app.globalData.url
var template = require('../../../template/template.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: "33%",
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    name:'游客',
    school:'暂无',
    xuejinumber:'暂无',
    ifName:true,
    home: {
      "current": 0,
      "pagePath": "/pages/home/ptwords/ptwords",
      "iconPath": "/images/icons/icona_a.png",
      "text": "习字"
    },
    photo: {
      "current": 0,
      "pagePath": "/pages/photo/goTest/goTest",
      "iconPath": "/images/icons/icon1.png",
      "text": "评测"
    },
    mine: {
      "current": 0,
      "pagePath": "/pages/my/visitor/mine/mine",
      "iconPath": "/images/icons/icon4.png",
      "text": "我的"
    },
    // find: {
    //   "current": 0,
    //   "pagePath": "/pages/find/find",
    //   "iconPath": "/images/icons/icon3.png",
    //   "text": "随堂练习"
    // },
    store: {
      "current": 0,
      "pagePath": "/pages/store/storeHome/storeHome",
      "iconPath": "/images/icons/store_1.png",
      "text": "助学"
    },
    // display: 'none',
    // displayT: "block",
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
      }
    })

    wx.getStorage({
      key: 'student_id',
      success: function (resl) {
        wx.request({
          url: base_url+'/getStudentInfo/',
          method: 'POST',
          data: {
            student_id: resl.data
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            that.setData({
              name: res.data['name'],
              student_id: resl.data
            })
            that.setData({
              school: res.data['schoolName']
            })
            that.setData({
              xuejinumber: res.data['studentCode']
            })

          }
        })
      },
      fail:function(res){

      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tosettings:function(){
    var that = this;
    wx.navigateTo({
      url: '../editInfo/editInfo',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
    // that.setData({
    //   ifName:false
    // })
  },
  cancel:function(){
    var that = this;
    that.setData({
      ifName: true
    })
  },
  setNameValue:function(res){
    var that = this;
    that.setData({
      nameValue:res.detail.value
    })
  },
  setStudentCodeValue: function (res) {
    var that = this;
    that.setData({
      studentCodeValue: res.detail.value
    })
  },
  confirm:function(res){
    var that = this;
    var student_id = 
    wx.request({
        url: base_url+'/setStudentInfo/',
      method: 'POST',
      data: {
        nameValue: that.data.nameValue,
        studentCodeValue: that.data.studentCodeValue,
        student_id: that.data.student_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (resl) {
        console.log(resl)
        if(resl.data['flag'] == true){
          that.setData({
            ifName: true
          })
          wx.navigateTo({
            url: '../mine/mine',
            success: function (res) {
              console.log(res)
            },
            fail: function () {
            }
          })
        }
      }
    })
    
  },
  gotoLeads:function(){
    wx.navigateTo({
      url: '../../leads/leads',
      success:function(res){
        console.log(res)
      },
      fail:function(){

      }
    })
  },
  gotoOptions:function(){
    wx.navigateTo({
      url: '../../options/options',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },

  gotoHistories:function(){
    var that = this

    wx.request({
      url: base_url + '/getStudentHistory/',
      method: 'POST',
      data: {
        auth: that.data.auth,
        student_id: that.data.student_id,
        // teacher_id: that.data.teacher_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data['flag'] == true) {
          wx.setStorage({
            key: 'histort_datas',
            data: res.data['lst'],
            success: function () {
              wx.navigateTo({
                url: '../histories/histories',
                success: function (res) {
                }
              })
            },

          })
          // that.setData({
          //   histort_datas: res.data['lst']
          // })
        }
      }
    })
    console.log(that.data.histort_datas)
  },
  gotoClass:function(){
    var that = this
    wx.request({
      url: base_url+'/getVisClassInfo/',
      method: 'POST',
      data: {
        student_id: that.data.student_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data['flag'] == true) {
          wx.setStorage({
            key: 'class_dic',
            data: res.data['class_dic'],
            success: function (res) {
              wx.navigateTo({
                url: '../myClass/myClass',
                success: function (res) {
                  console.log(res)
                },
                fail: function () {
                }
              })
            }
          })
          
        } else {
          
          wx.navigateTo({
            url: '../addClass/addClass',
            success: function (res) {
              console.log(res)
            },
            fail: function () {

            }
          })
        }
      }
    })
    
  },
  gotoMyAll:function(){
    wx.navigateTo({
      url: '../myAllExercises/myAllExercises',
      success: function (res) {
        console.log(res)
      },
      fail: function () {

      }
    })
  }
})